﻿using System;
using System.Reflection;
using System.Runtime.Serialization;

using Dynamic.Core.Reflection;

namespace Dynamic.Core.Utility
{
    /// <summary>
    /// 自定义属性通用类
    /// </summary>
    public static class CustomAttributeUtility
    {
        /// <summary>
        /// 是否可以序列化
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsSerializable(Type type)
        {
            return (GetSerializableAttribute(type.GetCustomAttributes(true)) == null) ? false : true;
        }

        /// <summary>
        /// 是否DataContractAttribute
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsDataContract(Type type)
        {
            return (GetDataContractAttribute(type.GetCustomAttributes(true)) == null) ? false : true;
        }

        /// <summary>
        /// 是否DataMemberAttribute
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        public static bool IsDataMember(PropertyInfo pi)
        {
            return (GetDataMemberAttribute(pi.GetCustomAttributes(true)) == null) ? false : true;
        }

        /// <summary>
        /// 得到SerializableAttribute
        /// </summary>
        /// <param name="customAttributes"></param>
        /// <returns></returns>
        public static SerializableAttribute GetSerializableAttribute(object[] customAttributes)
        {
            return (SerializableAttribute)GetCustomAttribute(customAttributes, typeof(SerializableAttribute));
        }

        /// <summary>
        /// 得到DataContractAttribute
        /// </summary>
        /// <param name="customAttributes"></param>
        /// <returns></returns>
        public static DataContractAttribute GetDataContractAttribute(object[] customAttributes)
        {
            return (DataContractAttribute)GetCustomAttribute(customAttributes, typeof(DataContractAttribute));
        }

        /// <summary>
        /// 得到DataMemberAttribute
        /// </summary>
        /// <param name="customAttributes"></param>
        /// <returns></returns>
        public static DataMemberAttribute GetDataMemberAttribute(object[] customAttributes)
        {
            return (DataMemberAttribute)GetCustomAttribute(customAttributes, typeof(DataMemberAttribute));
        }

        /// <summary>
        /// 得到自定义属性
        /// </summary>
        /// <param name="pi"></param>
        /// <param name="attributeType"></param>
        /// <returns></returns>
        public static object GetCustomAttribute(PropertyInfo pi, Type attributeType)
        {
            return GetCustomAttribute(pi.GetCustomAttributes(true), attributeType);
        }

        /// <summary>
        /// 得到自定义属性值
        /// </summary>
        /// <param name="pi"></param>
        /// <param name="attributeType"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object GetCustomAttributeValue(PropertyInfo pi, Type attributeType, string propertyName)
        {
            return GetCustomAttributeValue(pi.GetCustomAttributes(attributeType, true), attributeType, propertyName);
        }

        /// <summary>
        /// 得到自定义属性
        /// </summary>
        /// <param name="customAttributes"></param>
        /// <param name="attributeType"></param>
        /// <returns></returns>
        public static object GetCustomAttribute(object[] customAttributes, Type attributeType)
        {
            if (customAttributes == null || customAttributes.Length < 1)
            {
                return null;
            }

            foreach (var item in customAttributes)
            {
                if (item.GetType().Name == attributeType.Name)
                {
                    return item;
                }
            }

            return null;
        }

        /// <summary>
        /// 得到自定义属性值
        /// </summary>
        /// <param name="customAttributes"></param>
        /// <param name="attributeType"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object GetCustomAttributeValue(object[] customAttributes, Type attributeType, string propertyName)
        {
            object reval = GetCustomAttribute(customAttributes, attributeType);
            if (reval == null)
            {
                return null;
            }

            PropertyInfo[] properties = reval.GetType().GetProperties();

            foreach (PropertyInfo item in properties)
            {
                if (item.Name == propertyName)
                {
                    return item.FastGetValue(reval);
                }
            }

            return null;
        }
        //TODO:通过自定义属性得到所有属性，得到某个属性
    }
}
